// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RGBDCameraAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Vision.Runtime.Cameras
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Threading.Tasks;

    using Microsoft.Robotics.Numerics;
    using Microsoft.Robotics.Runtime;
    using Microsoft.Robotics.Vision.Cameras;
    using Microsoft.Robotics.Vision.Cameras.Kinect;

    /// <summary>
    /// Class that contains a synchronized depth and color image
    /// </summary>
    /// <typeparam name="TColorParameters">The color camera parameters</typeparam>
    /// <typeparam name="TDepthParameters">The depth camera parameters</typeparam>
    [DataContract]
    public class RGBDCameraAgent<TColorParameters, TDepthParameters> : SynchronizedConsumerProducerAgent<ColorCameraAgentMessage<TColorParameters>, DepthCameraAgentMessage<TDepthParameters>, RGBDCameraMessage<TColorParameters, TDepthParameters>>
        where TColorParameters : ColorParameters
        where TDepthParameters : DepthParameters
    {
        /// <summary>
        /// Name of the agent that produces color images
        /// </summary>
        [DataMember]
        private string colorProducerName;

        /// <summary>
        /// Name of the agent that produces depth images
        /// </summary>
        [DataMember]
        private string depthProducerName;

        /// <summary>
        /// Type of alignment
        /// </summary>
        [DataMember]
        private Type alignmentType;

        /// <summary>
        /// Initializes a new instance of the RGBDCameraAgent class
        /// </summary>
        /// <param name="name">Name of the agent</param>
        /// <param name="colorProducer">producer of color images</param>
        /// <param name="depthProducer">producer of depth images</param>
        /// <param name="alignmentType">The type of class that aligns the images</param>
        public RGBDCameraAgent(
            string name,
            IProducer<ColorCameraAgentMessage<TColorParameters>> colorProducer,
            IProducer<DepthCameraAgentMessage<TDepthParameters>> depthProducer,
            Type alignmentType = null) 
            : base(name, 33)
        {
            this.colorProducerName = colorProducer.Name;
            this.depthProducerName = depthProducer.Name;
            this.alignmentType = alignmentType;
        }

        /// <summary>
        /// Gets the name of the first producer
        /// </summary>
        protected override string ProducerName1
        {
            get { return this.colorProducerName; }
        }

        /// <summary>
        /// Gets the name of the second producer
        /// </summary>
        protected override string ProducerName2
        {
            get { return this.depthProducerName; }
        }

        /// <summary>
        /// Called when a depth and color message are synchronized
        /// </summary>
        /// <param name="color">The color image</param>
        /// <param name="depth">The depth image</param>
        protected override void Receive(ColorCameraAgentMessage<TColorParameters> color, DepthCameraAgentMessage<TDepthParameters> depth)
        {
            Publisher.Post(new RGBDCameraMessage<TColorParameters, TDepthParameters>(color.OriginatingTime, color, depth, this.alignmentType));
        }
    }
}
